package co.yixiang.business.user

import cn.hutool.core.bean.BeanUtil
import co.yixiang.annotation.AnonymousAccess
import co.yixiang.api.ApiResult
import co.yixiang.business.attention.AttentionService
import co.yixiang.business.like.LikeService
import co.yixiang.business.visit.VisitService
import co.yixiang.modules.user.service.YxUserService
import co.yixiang.util.getApiResult
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.lang.Exception

@ApiModel("用户信息")
data class UserDetailVo(

    @ApiModelProperty("用户id") var uid: Long? = null,
    @ApiModelProperty("用户背景图片") var backgroundPic: String? = "",
    @ApiModelProperty("真实姓名") var realName: String? = "",
    @ApiModelProperty("用户头像") var avatar: String? = "",
    @ApiModelProperty("性别") var sex: Int? = 0,
    @ApiModelProperty("三坑") var sanKeng: String? = null,
    @ApiModelProperty("本体标签") var self: String? = null,
    @ApiModelProperty("个人简介") var mark: String? = null,
    @ApiModelProperty("获赞") var likeCount: Int? = 0,
    @ApiModelProperty("关注数量") var attentionCount: Int? = 0,
    @ApiModelProperty("粉丝数量") var fanceCount: Int? = 0,
    @ApiModelProperty("来访数量") var visitCount: Int? = 0,
)

@ApiModel("用户信息修改参数")
data class UpdateUserInfoDto(

    @ApiModelProperty("用户id") var id: Long? = null,
    @ApiModelProperty("真实姓名") var realName: String? = "",
    @ApiModelProperty("用户头像") var avatar: String? = "",
    @ApiModelProperty("用户背景图片") var backgroundPic: String? = "",
    @ApiModelProperty("性别") var sex: Int? = 0,
    @ApiModelProperty("三坑") var sanKeng: String? = null,
    @ApiModelProperty("本体标签") var self: String? = null,
    @ApiModelProperty("个人简介") var mark: String? = null,
)
interface UserHandle{

    /**
     * 获取app用户信息
     */
    fun getAppUserInfo(id: Long): ApiResult<*>

    /**
     * 修改用户背景图片
     */
    fun updateUserBackGroundPic(id: Long, backGroundPic: String): ApiResult<*>

    /**
     * 修改用户信息
     */
    fun updateUserInfo(updateUserInfoDto: UpdateUserInfoDto): ApiResult<*>
}

@Service
@Transactional(rollbackFor = [Exception::class])
open class UserHandleImp(@Autowired val yxUserService: YxUserService,
                         @Autowired val likeService: LikeService,
                         @Autowired val attentionService: AttentionService,
                         @Autowired val visitService: VisitService
                         ): UserHandle {

    override fun getAppUserInfo(id: Long): ApiResult<*> {

        return yxUserService.getById(id)?.let {

            UserDetailVo().apply {

                BeanUtil.copyProperties(it, this)
                likeCount = likeService.getMyLikeCount(id)
                attentionCount = attentionService.getMyAttentionList(id).count()
                fanceCount = attentionService.getMyFansCount(id)
                visitCount = visitService.getMyVisitsCount(id)
            }.getApiResult(200, "获取个人信息数据成功")
        } ?: getApiResult(201, "不存在当前用户")
    }

    override fun updateUserBackGroundPic(id: Long, backPic: String): ApiResult<*> {

        return yxUserService.getById(id)?.run {
            backgroundPic = backPic
            yxUserService.updateById(this).getApiResult(200, "修改用户背景图片成功")
        } ?: getApiResult(201, "当前用户不存在")
    }

    override fun updateUserInfo(updateUserInfoDto: UpdateUserInfoDto): ApiResult<*> {

        return yxUserService.getById(updateUserInfoDto.id)?.run {
            BeanUtil.copyProperties(updateUserInfoDto, this)
            yxUserService.updateById(this).getApiResult(200, "更新用户信息成功")
        } ?: getApiResult(200, "不存在当前用户")
    }


}

@RestController
@RequestMapping("/userHandle")
class UserHandleController(@Autowired val userHandle: UserHandle) {

    @ApiOperation("获取app用户信息")
    @PostMapping("/getAppUserInfo")
    @AnonymousAccess
    fun getAppUserInfo(id: Long): ApiResult<*> = userHandle.getAppUserInfo(id)


    @ApiOperation("修改用户背景图片")
    @PostMapping("/updateUserBackGroundPic")
    @AnonymousAccess
    fun updateUserBackGroundPic(id: Long, backPic: String): ApiResult<*> = userHandle.updateUserBackGroundPic(id, backPic)


    @ApiOperation("修改用户信息")
    @PostMapping("/updateUserInfo")
    @AnonymousAccess
    fun updateUserInfo(updateUserInfoDto: UpdateUserInfoDto): ApiResult<*> = userHandle.updateUserInfo(updateUserInfoDto)
}
